package Help.For.You.Other;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class DoumMay {
	// 경도구하기

	public String getGpsX(String addr) {

		URL url = null;

		HttpURLConnection con = null;

		InputStream is = null;

		SAXBuilder builder = null;

		Document doc = null;

		Element root = null;

		String result = null;

		try {

			url = new URL(
					"http://apis.daum.net/local/geo/addr2coordapikey=DAUM_LOCAL_DEMO_APIKEY&q="
							+ URLEncoder.encode(addr, "utf-8") + "&output=xml");

			// 다음오픈api주소좌표검색

			con = (HttpURLConnection) url.openConnection();

			is = con.getInputStream();

			builder = new SAXBuilder();

			doc = builder.build(is);

			root = doc.getRootElement();

			// 최상위 Element 값을 root에 저장

			result = root.getChild("item").getChildText("lng");

			// root하위에 item Element중 lng의 값을 result값에 저장

		} catch (Exception e) {

			e.printStackTrace();

		}

		return result;

	}

	// 위도구하기

	public String getGpsY(String addr) {

		URL url = null;

		HttpURLConnection con = null;

		InputStream is = null;

		SAXBuilder builder = null;

		Document doc = null;

		Element root = null;

		String result = null;

		try {

			url = new URL(
					"http://apis.daum.net/local/geo/addr2coordapikey=DAUM_LOCAL_DEMO_APIKEY&q="
							+ URLEncoder.encode(addr, "utf-8") + "&output=xml");

			con = (HttpURLConnection) url.openConnection();

			is = con.getInputStream();

			builder = new SAXBuilder();

			doc = builder.build(is);

			root = doc.getRootElement();

			result = root.getChild("item").getChildText("lat");

		} catch (Exception e) {

			e.printStackTrace();

		}

		return result;

	}

}
